/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.ui.actionlisteners;

import com.itextpdf.text.Document;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import project.jpdfexport.jpdf.JPDFExport;
import project.jpdftools.constants.Const;
import project.jpdftools.documentinfo.DocumentInfo;
import project.jpdftools.headerfooter.Footer;
import project.jpdftools.headerfooter.Header;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;
import project.jpdftools.other.JOpenFile;
import project.jpdftools.pagesetup.PageSetup;
import project.jpdftools.toolkit.Toolkit;
import project.jpdftools.ui.MainMenu;

/**
 *
 * @author Mido
 */
public class ExportAction implements ActionListener {

    public void actionPerformed(ActionEvent evt) {
        // make sure that there is something to export
        if (MainMenu.getInstance().getP_right().getComponentCount() != 0) {
            // ask the user to choose the file location.
            JFileChooser file_chooser = new JFileChooser();
            if (file_chooser.showSaveDialog(MainMenu.getInstance()) == JFileChooser.APPROVE_OPTION) {
                try {
                    // Document object to hold the data
                    Document document = new Document();

                    // export object
                    JPDFExport export_object = new JPDFExport();

                    // set the document properties before opening it
                    document = export_object.prepareDocument(document, PageSetup.getPaper_size(), PageSetup.getOrientation(), PageSetup.isUse_default_margins(),
                            PageSetup.getLeft_margin(), PageSetup.getRight_margin(), PageSetup.getTop_margin(), PageSetup.getBottom_margin());

                    // open the document
                    document = export_object.openDocument(document, file_chooser.getSelectedFile().getPath() + ".pdf");

                    // set document description
                    document = export_object.setDocumentDescription(document, DocumentInfo.getTitle(), DocumentInfo.getAuthor(),
                            DocumentInfo.getSubject(), DocumentInfo.getKeywords(), Const.program_version);

                    // set header and footer
                    document = export_object.setHeaderFooter(document, Header.getLeft_header(), Footer.getLeft_footer(), Header.getCenter_header(),
                            Footer.getCenter_footer(), Header.getRight_header(), Footer.getRight_footer());

                    // check if the user is using default font or not
                    Object[] options = {LocalizationManager.getCurrent_resource().getString("OPEN_FILE"),
                        LocalizationManager.getCurrent_resource().getString("DONT_OPEN_FILE")};

                    // loop on the arraylist of jpdf components and export every element of them
                    for (int i = 0; i < MainMenu.getInstance().getJpdf_data().size(); i++) {
                        MainMenu.getInstance().getJpdf_data().get(i).export(document);
                    }

                    // close the document
                    document.close();

                    // ask the user if he/she wants to open the exported file
                    JOpenFile.showOptionDialog(LocalizationManager.getCurrent_resource().getString("MAIN_MENU_EXPORT_PDF_SUCCESS"),
                            LocalizationManager.getCurrent_resource().getString("SUCCESS"), options, file_chooser.getSelectedFile() + ".pdf");
                    LoggerManager.getLogger().log(Level.INFO, "File exported successfully to: {0}", file_chooser.getSelectedFile() + ".pdf");
                } catch (Exception e) {
                    // show an error message
                    JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MAIN_MENU_EXPORT_PDF_FAIL") + "\n\""
                            + e.getMessage() + "\"", LocalizationManager.getCurrent_resource().getString("ERROR"), JOptionPane.ERROR_MESSAGE);
                    LoggerManager.getLogger().log(Level.SEVERE, "An excption occurred. {0}", Toolkit.getStackTraceAsString(e.getCause()));
                }
            }
        } else {
            // show an error message
            JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MAIN_MENU_EXPORT_PDF_NO_COMPONENT_ERROR"),
                    LocalizationManager.getCurrent_resource().getString("ERROR"), JOptionPane.ERROR_MESSAGE);
            LoggerManager.getLogger().log(Level.INFO, "No components to export");
        }
    }
}
